package com.kingdee.jdy.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.jdy.util.HttpUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

/**
 * 凭证
 */
public class VoucherController {

    public static long saveVoucher(HashMap<String, Object> urlParam, HashMap<String, Object> headerParam)
        throws Exception {

        long account_id = 0;
        JSONObject jsonStock = VoucherController.prepareVoucherParam(urlParam, headerParam);
        JSONObject resStock = HttpUtil
            .doPost("http://api.kingdee.com/jdy/gl/voucher_save", urlParam, headerParam, jsonStock.toJSONString());

        if (resStock != null) {
            JSONArray datas = resStock.getJSONArray("data");
            if (datas != null && datas.size() > 0) {
                JSONObject data = datas.getJSONObject(0);

                String success = data.getString("success");
                if (success.equalsIgnoreCase("true")) {
                    account_id = data.getJSONArray("successPkIds").getLongValue(0);
                    System.out.println("同步凭证成功！account_id=" + account_id);
                } else {
                    System.out.println("同步凭证失败！");
                }
            } else {
                System.out.println("同步凭证失败！");
            }
        } else {
            System.out.println("同步凭证失败！");
        }

        return account_id;
    }

    public static JSONObject prepareVoucherParam(HashMap<String, Object> urlParam, HashMap<String, Object> headerParam)
        throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = simpleDateFormat.format(new Date());


        JSONObject item = new JSONObject();
        item.put("id", null);
        item.put("date", dateStr);
        item.put("groupid_id", "1");
        item.put("remark", "demo测试凭证");

        JSONArray entries = new JSONArray();
        JSONObject entry1 = new JSONObject();
        entry1.put("id", null);
        entry1.put("explanation", "demo测试凭证摘要1");
        entry1.put("account_id", 1);//库存现金科目
        entry1.put("dc", 1);
        entry1.put("debitamount", 10);

        JSONObject entry2 = new JSONObject();
        entry2.put("id", null);
        entry2.put("explanation", "demo测试凭证摘要2");
        entry2.put("account_id", AccountController.saveAccount(urlParam, headerParam));//这里新增一个带辅助核算的科目
        entry2.put("dc", -1);
        entry2.put("creditamount", 10);
        JSONArray assistArray = new JSONArray();
        JSONObject assist = new JSONObject();
        assist.put("id", CustomerController.getAnyOneCustomer(urlParam, headerParam));//客户辅助核算
        assist.put("type", "bd_customer");
        assist.put("bd_auxinfo_type_id", null);
        assistArray.add(assist);
        entry2.put("assist", assistArray);

        entries.add(entry1);
        entries.add(entry2);

        item.put("entries", entries);

        JSONArray items = new JSONArray();
        items.add(item);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("items", items);

        return jsonObject;
    }



}
